package com.cskaoyan.service;

import com.cskaoyan.bean.po.MarketStorage;
import com.cskaoyan.bean.vo.AdminStorageCreateVO;
import com.cskaoyan.mapper.MarketStorageMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author zhuqiushi
 * @since 2022/09/07 10:46
 */
@Service
public class AdminStorageServiceImpl implements AdminStorageService {
    @Autowired
    MarketStorageMapper marketStorageMapper;

    @Override
    public int create(AdminStorageCreateVO createVO) {
        //负责插入图片
        MarketStorage marketStorage = new MarketStorage();
        marketStorage.setKey(createVO.getKey());
        marketStorage.setName(createVO.getName());
        marketStorage.setType(createVO.getType());
        marketStorage.setSize(createVO.getSize());
        marketStorage.setUrl(createVO.getUrl());
        Date currentTime = new Date(System.currentTimeMillis());
        marketStorage.setAddTime(currentTime);
        marketStorage.setUpdateTime(currentTime);
        marketStorage.setDeleted(false);
        createVO.setAddTime(currentTime);
        createVO.setUpdateTime(currentTime);
        try {
            //调用selective插入，获取自增的id
            marketStorageMapper.insertSelective(marketStorage);
        } catch (Exception e) {
            //发生异常插入失败
            e.printStackTrace();
            return 404;
        }
        //id赋值给vo
        createVO.setId(marketStorage.getId());
        return 0;
    }
}
